<?php

namespace App\Http\Controllers;

use App\Models\Files;
use App\Models\Lists;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Lib\ResBase;
use App\Http\Lib\isAdmin;

class RootController extends Controller
{
    //审核用户、添加权限
    public function root(Request $request)
    {
        $id = $request->input('userId');
        $userres = isAdmin::isadmin($id);
        if($userres){
            return $userres;
        }
        $newUser = $request->input('newUserId');
        $root = $request->input('root');
        if(!$id || !$root || !$newUser){
            return ResBase::ApiRes(401,'缺少参数','failed');
        }
        $user = Users::where('user_id',$newUser)->first();

        if($user->status ===1){
            return ResBase::ApiRes(402,'该账号已启用');
        } elseif (!$user){
            return ResBase::ApiRes(403,'该用户不存在');
        } else {
            $user->status = 1;
            $user->root = $root;
            $res = $user->save();
            if(!$res){
                return  ResBase::ApiRes(400,'修改失败');
            } else {
                return  ResBase::ApiRes(200,$user);
            }
        }
    }
    //修改用户权限
    public function editUserRoot(Request $request)
    {
        $id = $request->input('userId');
        $target = $request->input('targetId');
        $root = $request->input('root');
        $userres = isAdmin::isadmin($id);
//        var_dump($id,$target,$root);
        if($userres){
            return $userres;
        }
        $targetuser = Users::where('user_id',$target)->first();
        if(!$id || !$target || !$root){
            return ResBase::ApiRes(403,'缺少参数','failed');
        }
        if(!$targetuser){
            return ResBase::ApiRes(401,'该用户不存在');
        } else if ($targetuser->root === $root){
            return ResBase::ApiRes(402,'修改后的权限等级不能与之前一致');
        } else {
            $targetuser->root = $root;
            $res = $targetuser->save();
            if(!$res){
                return  ResBase::ApiRes(400,'修改失败');
            } else {
                return  ResBase::ApiRes(200,$targetuser);
            }
        }
    }
    //修改文件权限
    public function editFileRoot(Request $request){
        $id = $request->input('userId');
        $file = $request->input('fileId');
        $root = $request->input('root');
        $userres = isAdmin::isadmin($id);
        if($userres){
            return $userres;
        }
        $targetfile = Files::where('id',$file)->first();
        if(!$id || !$file || !$root){
            return ResBase::ApiRes(403,'缺少参数','failed');
        }
        if(!$targetfile){
            return ResBase::ApiRes(401,'该文件不存在');
        } else if ($targetfile->root === $root){
            return ResBase::ApiRes(402,'修改后的权限等级不能与之前一致');
        } else {
            $targetfile->root = $root;
            $res = $targetfile->save();
            if(!$res){
                return  ResBase::ApiRes(400,'修改失败');
            } else {
                return  ResBase::ApiRes(200,$targetfile);
            }
        }
    }

    //修改文件夹权限
    public function editListRoot(Request $request){
        $id = $request->input('userId');
        $list = $request->input('listId');
        $root = $request->input('root');
        $userres = isAdmin::isadmin($id);
        if($userres){
            return $userres;
        }
        $targetlist = Lists::where('id',$list)->first();
        if(!$id || !$list || !$root){
            return ResBase::ApiRes(403,'缺少参数','failed');
        }
        if(!$targetlist){
            return ResBase::ApiRes(401,'该文件不存在');
        } else if ($targetlist->root === $root){
            return ResBase::ApiRes(402,'修改后的权限等级不能与之前一致');
        } else {
            $targetlist->root = $root;
            $res = $targetlist->save();
            if(!$res){
                return  ResBase::ApiRes(400,'修改失败');
            } else {
                return  ResBase::ApiRes(200,$targetlist);
            }
        }
    }
}
